$(function () {
	getChildrenList();
})

function getChildrenList() {
	slf.ajax({
		url : "/myCenter/getUserChildrenCourse",
		param : {
			type:"0"
		},
		success : function(data){
			console.log(data)
			console.log("获取返回宝宝信息")
			if(data.res <=0){
				var html = "";
				html += '<div class="bg-fff am-margin-top-xs am-padding-xs"><img src="../img/zwsj.png" alt="img" style="width:100%"></div>';
			}
			if (data.res > 0) {
				var html = "";
                var children = data.obj;
                console.log(data.obj)
                children.forEach(function (item, index) {
                	html+='<li class="w100b borT-EF" style="margin-bottom:10px">'
                	html+='<div class="titleLi borB-EF bg-FFFBF2 pad-10">' 
                	html+='<span class="borLeft">姓名：'+ item.name +'</span>' 
                	if (item.sequence == 0) {
                        //默认选中
                		html+='<div class="w35 childradio posi-rela am-margin-top-sm radios"><input type="radio" id="'+ item.childrenId +'" checked="true" name="childradio"/><label for="'+ item.childrenId +'">选为默认</label> </div>'
                    } else {
                        //非默认选中
                    	html+='<div class="w35 childradio posi-rela am-margin-top-sm radios"><input type="radio" id="'+ item.childrenId +'" name="childradio"/><label for="'+ item.childrenId +'">选为默认</label> </div>'
                        html += "</div>";
                    }  
                	html+='</div>'  
                	html+='<div class="brithMessDiv bg-FFFBF2 pad-10 borB-EF">'  
                	html+='<span class="brithMess" style="width:40%">生日：'+ item.birthdayStr+'</span>' 
                	if(item.sex == 0){
                		item.sex = "男"
                	}else{
                		item.sex = "女"
                	}
                	html+='<span class="brithMess text-center">性别：'+item.sex+'</span>'     
                	html+='<span class="brithMess text-right co-39" onclick="updataChild('+item.userId+','+item.childrenId+')" >编辑</span>'     
                	html+='</div>' 
                	html+='<div class="titleLi borT-EF borB-EF bg-FFFBF2 pad-10">' 
                	html+='<span class="borLeft">课程进度</span>'
            		
                	console.log(item.schoolCoursePage.length);
                	if(item.schoolCoursePage.length==0&&item.childrenCoursePage.length==0){
                		
                	}else{
                		html+='<span class="moreDiv co-39" onclick="getMore('+item.childrenId+')">查看更多></span>' 
                	} 
                	html+='</div>' 
                	html+='<div class="classDiv bg-FFFBF2 pad-10 borB-EF">' 
                	if(item.schoolCoursePage.length==0&&item.childrenCoursePage.length==0){
                		html+='<div class="classLi">暂无课程进度</div>' 
                	}else if(item.childrenCoursePage.length>1){
                		for(var i=0;i<2;i++){
                			html+='<div class="classLi">'+item.childrenCoursePage[i].name+'（剩余'+item.childrenCoursePage[i].count+'节）</div>'    
                        }
                	}else if(item.childrenCoursePage.length==1&&item.schoolCoursePage.length==1){
                		html+='<div class="classLi">'+item.childrenCoursePage[0].name+'（剩余'+item.childrenCoursePage[0].count+'节）</div>'    
                		html+='<div class="classLi">'+item.schoolCoursePage[0].name+'（共计1节）</div>'    
                	}else if(item.childrenCoursePage.length==1&&item.schoolCoursePage.length==0){
                		html+='<div class="classLi">'+item.childrenCoursePage[0].name+'（剩余'+item.childrenCoursePage[0].count+'节）</div>'    
                	}else if(item.childrenCoursePage.length==0&&item.schoolCoursePage.length>1){
                		for(var i=0;i<2;i++){
                			html+='<div class="classLi">'+item.schoolCoursePage[i].name+'（共计1节）</div>'    
                        }
                	}else if(item.childrenCoursePage.length==0&&item.schoolCoursePage.length==1){
                		html+='<div class="classLi">'+item.schoolCoursePage[0].name+'（共计1节）</div>' 
                	}  
                	html+='</div></li>'
                })
                console.log(7);
                $("#list").append(html);
                console.log(8);
				$("body").on("click", "#list input", function () {
                    var childrenId = $(this).attr("id")
                    slf.ajax({
                    	url : "/user/setChildren",
                    	param :{json: childrenId},
                    	success : function(data){
                    		if(data.res>0){
                    			console.log("修改默认值成功")
                    		}else{
                    			console.log(data.resMsg)
                    		}
                    	}
                    });
				});
			}
		}
	});
}
function updataChild(userId,childrenId){
	window.location.href = "add_child.html?userId="+userId+"&childrenId="+childrenId+""
}
function getMore(childrenId){
	window.location.href = "curProgress.html?childrenId="+childrenId+""
}
